package com.tools.cleanmaster.ui.card;

import android.app.Activity;
import android.text.Html;
import android.view.View;

import com.tools.ad.EntranceType;
import com.tools.ad.util.GARecordUtils;
import com.tools.cleanmaster.CleanApplication;
import com.tools.cleanmaster.CleanGAConstant;
import com.tools.cleanmaster.R;
import com.tools.cleanmaster.store.GlobalConfigSharedPrefManager;
import com.tools.cleanmaster.store.ResultPageSharedPrefManager;
import com.tools.cleanmaster.ui.card.factory.CardDataHelper;
import com.tools.cleanmaster.ui.card.factory.CardListAdapter;
import com.tools.cleanmaster.ui.card.viewholder.ACardViewHolder;
import com.tools.cleanmaster.ui.card.viewholder.CommonCardViewHolder;
import com.tools.cleanmaster.utils.LogHelper;
import com.tools.cleanmaster.utils.StringUtils;

import java.util.Locale;

/**
 * Created by lianzhan on 15/10/31.
 */
public class TrashCardItem extends ACardItem {
    private TrashCardListener mTrashListener;

    public TrashCardItem(EntranceType entranceType, int pos) {
        super(entranceType, pos);
    }

    @Override
    public CardViewType getCardViewType() {
        return CardViewType.TRASH;
    }

    @Override
    public boolean needShow(EntranceType entranceType) {
        long size = GlobalConfigSharedPrefManager.getLeftOverSize(CleanApplication.getInstance());
        boolean isShow = size > 0 && ResultPageSharedPrefManager.isCloudCardSwitch(CleanApplication.getInstance(), getCardName());
        boolean lastCleanAllDefault = ResultPageSharedPrefManager.isLastCleanAllDefault();
        LogHelper.i(CardDataHelper.TAG, isShow && !lastCleanAllDefault ? "再次清理卡片展示" : "再次清理卡片不展示=size=" + size + " 开关=" + ResultPageSharedPrefManager
                .isCloudCardSwitch(CleanApplication.getInstance(), getCardName()) + " 用户清理时，是否进行默认勾选操作=" + lastCleanAllDefault);
        return isShow && !lastCleanAllDefault;
    }

    @Override
    public String getCardName() {
        return CardType.TRASH.key;
    }

    @Override
    public void bindCardView(final Activity activity, ACardViewHolder viewHolder, CardListAdapter cardAdapter, final int position) {
        super.bindCardView(activity, viewHolder, cardAdapter, position);
        CommonCardViewHolder cardViewHolder = (CommonCardViewHolder) viewHolder;
        long size = GlobalConfigSharedPrefManager.getLeftOverSize(CleanApplication.getInstance());
        cardViewHolder.mLogo.setBackgroundResource(R.drawable.card_func_trash);
        cardViewHolder.mTitle.setText(R.string.trash_clean_continue);
        cardViewHolder.mDesc.setText(Html.fromHtml(activity.getString(R.string.trash_clean_continue_content,
                StringUtils.getSizeString(size))));
        cardViewHolder.mExecute.setText(R.string.trash_clean_check);
        cardViewHolder.mExecute.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                doClick(activity, position);
            }
        });
        cardViewHolder.mItemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                doClick(activity, position);
            }
        });
    }

    private void doClick(Activity activity, int pos) {
        if (mTrashListener != null) {
            GARecordUtils.report(imContext, CleanGAConstant.C_FUNC, CleanGAConstant.A_CLICK,
                    String.format(Locale.ENGLISH, CleanGAConstant.L_CARD_TRASH_CLEAN, String.valueOf(mEntranceType.getName())));
            mTrashListener.onScanTrash();
        }
    }

    public void setTrashListener(TrashCardListener trashListener) {
        this.mTrashListener = trashListener;
    }

    @Override
    public void onActivityDestroy() {
        if (mTrashListener != null) {
            mTrashListener = null;
        }
        LogHelper.i(CardDataHelper.TAG, "再次清理卡片执行=onActivityDestroy");
    }
}
